﻿using ShengYueWorkShop.BLL.DataModel;
using ShengYueWorkShop.BLL.PublicHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ShengYueWorkShop.BLL.Tcp
{
    public class ServerHelper
    {
        #region 变量
        /// <summary>
        /// 监听服务端
        /// </summary>
        public TcpListener _TcpListener;
        /// <summary>
        /// 日志输出控件
        /// </summary>
        public RichTextBox _RichBox;
        /// <summary>
        /// Token秘钥
        /// </summary>
        private string _Token;
        /// <summary>
        /// 客户端集合
        /// </summary>
        private List<TcpClient> _TcpClientList;
        private List<Thread> _TcpThreadList;
        #endregion
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="ServerIP">IP</param>
        /// <param name="ServerPort">端口</param>
        /// <param name="Token">秘钥</param>
        /// <param name="richBox">日志控件</param>
        /// <param name="StarBtn">按钮</param>
        public void ListenerStar(IPAddress ServerIP, int ServerPort, string Token, RichTextBox richBox)
        {
            try
            {
                _Token = Token;
                _RichBox = richBox;
                _TcpListener = new TcpListener(ServerIP, ServerPort);
                _TcpListener.Start();
                LogWrite("", "服务运行中……");
                //StarBtn.Text = "停止服务";
                LogWrite("", "等待客户端接入……");
                //初始话变量
                _TcpClientList = new List<TcpClient>();
                _TcpThreadList = new List<Thread>();

                //开始循环等待处理客户端数据
                while (true)
                {
                    TcpClient fTcpClient = _TcpListener.AcceptTcpClient();
                    _TcpClientList.Add(fTcpClient);
                    int fClientIndex = _TcpClientList.Count - 1;

                    //开启新线程处理接入客户端的数据
                    Thread TProcClientMsg = new Thread(new ParameterizedThreadStart(ProcClient));
                    _TcpThreadList.Add(TProcClientMsg);
                    TProcClientMsg.Start(fClientIndex);
                    TProcClientMsg.IsBackground = true;
                }
            }
            catch (SocketException ex)
            {
                LogWrite("", ex.ToString());
                LogWrite("", "服务已经退出");
                _TcpListener?.Stop();
                //StarBtn.Text = "启动服务";
                //ListenerStop();
            }
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        public void ListenerStop()
        {
            LogWrite("", "正在退出");
            foreach (Thread fthread in _TcpThreadList)
            {
                fthread?.Abort();
            }
            foreach (TcpClient tClient in _TcpClientList)
            {
                tClient?.Close();
            }
            if (_TcpListener != null)
            {
                _TcpListener?.Stop();
                _TcpListener = null;
            }
            LogWrite("", "服务已停止");
        }
        /// <summary>
        /// 处理客户端数据
        /// </summary>
        private void ProcClient(object fobject)
        {
            int fClientIndex = Convert.ToInt16(fobject);
            TcpClient fClient = ((TcpClient)_TcpClientList[fClientIndex]);
            string fTcpClientPoint = fClient.Client.RemoteEndPoint.ToString();
            //定义Token秘钥表示，false表示没有通过验证
            bool TokenPass = false;
            //客户端当前目录
            string ClientDirectoryPath = "";
            //功能是否已经正确执行
            bool flag = false;
            if (fClient != null || fClient.Client.Connected)
            {
                do
                {
                    //循环处理数据
                    LogWrite(fTcpClientPoint, "等待客户端请求指令");
                    if (fClient.Client.Connected)
                    {
                        flag = ReadAndWirteMsg(fTcpClientPoint, fClientIndex, ref TokenPass, ref ClientDirectoryPath);
                    }
                    else
                    {
                        LogWrite(fTcpClientPoint, "客户端已经退出连接");
                        flag = false;
                    }
                }
                while (flag && TokenPass);

                if (!flag && fClient.Client.Connected)
                {
                    //执行失败但是客户端连接正常的,发送关闭连接
                    ServerSend(TcpOperationCommand.Closed, "", fClientIndex);
                }
            }
            else
            {
                //退出日志
                LogWrite(fTcpClientPoint, "连接异常");
            }
        }
        /// <summary>
        /// 读取数据，处理数据，并返回处理完成的数据
        /// </summary>
        /// <param name="pClientName">客户端名称</param>
        /// <param name="pStream">客户端数据流</param>
        /// <param name="pTokenPass">秘钥是否已经通过核对(内部处理该值后再返回)</param>
        /// <param name="pClientDirectoryPath">连接的客户端所在(服务)的当前目录</param>
        /// <returns></returns>
        private bool ReadAndWirteMsg(string pClientName, int pClientIndex, ref bool pTokenPass, ref string pClientDirectoryPath)
        {
            bool flag = false;
            try
            {
                string fClientMsg = "";
                // TcpReadStruct fReadStruct = ClientRead(pStream);
                TcpReadStruct fReadStruct = ServerRead(pClientIndex);
                if (fReadStruct.ReadStatus)
                {
                    LogWrite(pClientName, "操作指令:" + fReadStruct.TcpOperationCommand);
                    if (!pTokenPass)
                    {
                        #region 验证Token秘钥是否正确
                        string fSendMsg = "";
                        if (fReadStruct.TcpOperationCommand == TcpOperationCommand.Token)
                        {
                            fClientMsg = Encoding.UTF8.GetString(fReadStruct.Message);
                            if (fClientMsg == _Token)
                            {
                                LogWrite(pClientName, "反馈：Token验证成功");
                                flag = true;//重置标志位位正确，可以继续下一个请求
                                pTokenPass = true;
                            }
                            else
                            {
                                LogWrite(pClientName, "反馈：Token验证失败");
                            }
                            ServerSend(TcpOperationCommand.Token, pTokenPass.ToString(), pClientIndex);
                            // ClientWrite(TcpOperationCommand.Token, pTokenPass.ToString(), pStream);
                        }
                        else
                        {
                            fSendMsg = "反馈：请先验证Token";
                            LogWrite(pClientName, fSendMsg);
                            ServerSend(TcpOperationCommand.Message, fSendMsg, pClientIndex);
                            //ClientWrite(TcpOperationCommand.Message, fSendMsg, pStream);
                        }
                        #endregion
                    }
                    else
                    {
                        #region 客户端操作指令应对
                        TcpWriteStruct fStruct = new TcpWriteStruct();
                        if (fReadStruct.TcpOperationCommand == TcpOperationCommand.GetList)
                        {
                            fStruct = GetList(pClientDirectoryPath);
                            if (pClientDirectoryPath.Length == 0)
                            {
                                LogWrite(pClientName, "获取 盘符 列表");
                            }
                            else
                            {
                                LogWrite(pClientName, "获取 " + pClientDirectoryPath + " 目录列表");
                            }

                        }
                        else if (fReadStruct.TcpOperationCommand == TcpOperationCommand.EnterDir)
                        {
                            //进入指定下级目录
                            fClientMsg = Encoding.UTF8.GetString(fReadStruct.Message);
                            fStruct = EnterDirectory(fClientMsg, ref pClientDirectoryPath);
                            LogWrite(pClientName, "进入目录: " + fClientMsg);
                            LogWrite(pClientName, "当前工作目录: " + pClientDirectoryPath);
                        }
                        else if (fReadStruct.TcpOperationCommand == TcpOperationCommand.ComeOutDir)
                        {
                            //返回上级目录
                            fStruct = ComtOutDirectory(ref pClientDirectoryPath);
                            LogWrite(pClientName, "返回上一级目录");
                            LogWrite(pClientName, "当前工作目录: " + pClientDirectoryPath);
                        }
                        else if (fReadStruct.TcpOperationCommand == TcpOperationCommand.GetPicZip)
                        {
                            //获取指定的图片文件，并将其压缩后传送给客户端
                            fClientMsg = Encoding.UTF8.GetString(fReadStruct.Message);
                            fStruct = GetPicZip(fClientMsg, pClientDirectoryPath);
                            LogWrite(pClientName, string.Format("压缩图片:{0}", fClientMsg));
                        }
                        else if (fReadStruct.TcpOperationCommand == TcpOperationCommand.Closed)
                        {
                            //接收客户端的关闭指令
                            return flag;
                        }
                        else
                        {
                            //没有存在已有的指令
                            fStruct.IsByte = false;
                            fStruct.TcpOperationCommand = TcpOperationCommand.Message;
                            fStruct.Message = string.Format("{0}未知操作指令", TcpOperationCommand.Message);
                            LogWrite(pClientName, fStruct.Message.ToString());
                        }

                        //发送回馈消息
                        if (fStruct.TcpOperationCommand.Length > 0)
                        {
                            if (fStruct.IsByte)
                            {
                                ServerSend(fStruct.TcpOperationCommand, (byte[])fStruct.Message, pClientIndex);
                                //ClientWrite(fStruct.TcpOperationCommand, (byte[])fStruct.Message, pStream);
                            }
                            else
                            {
                                ServerSend(fStruct.TcpOperationCommand, fStruct.Message.ToString(), pClientIndex);
                                //ClientWrite(fStruct.TcpOperationCommand, fStruct.Message.ToString(), pStream);
                            }
                            //LogWrite(pClientName, "反馈： " + fStruct.TcpOperationCommand);
                        }
                        flag = true;
                        #endregion
                    }
                }
                else
                {
                    flag = true;
                    ServerSend(TcpOperationCommand.Message, fReadStruct.ErrorMsg, pClientIndex);
                    // ClientWrite(TcpOperationCommand.Message, fReadStruct.ErrorMsg, pStream);
                    LogWrite(pClientName, fReadStruct.ErrorMsg);
                }
            }
            catch (Exception ex)
            {
                LogWrite(pClientName, ex.ToString());
            }
            return flag;
        }
        #region 读取、发送数据功能
        //private void ClientWrite(string pCommand, string pMsg, NetworkStream pStream)
        //{
        //    List<byte> fSendList = new List<byte>();
        //    byte[] fMsgBytes = Encoding.UTF8.GetBytes(pMsg);
        //    fSendList.AddRange(Encoding.UTF8.GetBytes(pCommand));
        //    fSendList.Add(TcpOperationCommand.ReadDataFenGe);
        //    fSendList.AddRange(Encoding.UTF8.GetBytes(fMsgBytes.Length.ToString()));
        //    fSendList.Add(TcpOperationCommand.ReadDataFenGe);
        //    fSendList.AddRange(fMsgBytes);
        //    ClientWriteBase(fSendList.ToArray(), pStream);
        //}
        //private void ClientWrite(string pCommand, byte[] pMsgByte, NetworkStream pStream)
        //{
        //    List<byte> fSendList = new List<byte>();
        //    fSendList.AddRange(Encoding.UTF8.GetBytes(pCommand));
        //    fSendList.Add(TcpOperationCommand.ReadDataFenGe);
        //    fSendList.AddRange(Encoding.UTF8.GetBytes(pMsgByte.Length.ToString()));
        //    fSendList.Add(TcpOperationCommand.ReadDataFenGe);
        //    fSendList.AddRange(pMsgByte);
        //    ClientWriteBase(fSendList.ToArray(), pStream);
        //}
        //private void ClientWriteBase(byte[] pBytes, NetworkStream pStream)
        //{
        //    //循环发送主数据包
        //    for (int i = 0; i < pBytes.Length; i += TcpOperationCommand.BytesMaxLenght)
        //    {
        //        byte[] fbytes = pBytes.Skip(i).Take(TcpOperationCommand.BytesMaxLenght).ToArray();
        //        pStream.Write(fbytes, 0, fbytes.Length);
        //    }
        //}
        private void ServerSend(string pCommand, string pMsg, int pClientIndex)
        {
            List<byte> fSendList = new List<byte>();
            byte[] fMsgBytes = Encoding.UTF8.GetBytes(pMsg);
            fSendList.AddRange(Encoding.UTF8.GetBytes(pCommand));
            fSendList.Add(TcpOperationCommand.ReadDataFenGe);
            fSendList.AddRange(fMsgBytes);
            byte[] fFullByte = fSendList.ToArray();
            ServerSendBase(pCommand, fFullByte, pClientIndex);
        }
        private void ServerSend(string pCommand, byte[] pMsgByte, int pClientIndex)
        {
            List<byte> fSendList = new List<byte>();
            fSendList.AddRange(Encoding.UTF8.GetBytes(pCommand));
            fSendList.Add(TcpOperationCommand.ReadDataFenGe);
            fSendList.AddRange(pMsgByte);
            byte[] fFullByte = fSendList.ToArray();
            ServerSendBase(pCommand, fFullByte, pClientIndex);
        }
        private void ServerSendBase(string pCommand, Byte[] pMsgByte, int pClientIndex)
        {
            TcpClient fTcpClient = (TcpClient)_TcpClientList[pClientIndex];
            if (fTcpClient.Client.Connected)
            {
                NetworkStream fStream = fTcpClient.GetStream();
                //设置超时时间
                fStream.ReadTimeout = TcpOperationCommand.TimeOut;
                fStream.WriteTimeout = TcpOperationCommand.TimeOut;
                //发送消息
                byte[] fBytelenght = BitConverter.GetBytes(pMsgByte.Length);
                fStream.Write(fBytelenght, 0, fBytelenght.Length);
                fStream.Write(pMsgByte, 0, pMsgByte.Length);
                LogWrite(fTcpClient.Client.RemoteEndPoint.ToString(), "数据发送成功");
            }
            else
            {
                LogWrite(fTcpClient.Client.RemoteEndPoint.ToString(), "发送失败，客户端连接异常");
            }
        }
        /// <summary>
        /// 接收客户端数据
        /// </summary>
        /// <param name="pDataLen">数据长度</param>
        /// <param name="pStream"></param>
        /// <returns></returns>
        //private TcpReadStruct ClientRead(NetworkStream pStream)
        //{
        //    //回馈数据集
        //    TcpReadStruct fReturnRead = new TcpReadStruct();
        //    fReturnRead.ReadStatus = false;
        //    fReturnRead.Lenght = 0;
        //    // 分包数据集
        //    List<byte> packetBuffer = new List<byte>();
        //    //已经接收的数据包长度
        //    int fReadDataLenght = 0;
        //    //单次接收的数据包长度
        //    int fbytesCount;
        //    try
        //    {
        //        do
        //        {
        //            byte[] TcpDataBytes = new byte[TcpOperationCommand.BytesMaxLenght];
        //            fbytesCount = pStream.Read(TcpDataBytes, 0, TcpDataBytes.Length);
        //            if (fReadDataLenght == 0)
        //            {
        //                List<byte[]> fBytesList = OtherFun.BytesSplit(TcpDataBytes, TcpOperationCommand.ReadDataFenGe);
        //                if (fBytesList.Count >= 3)
        //                {
        //                    for (int i = 0; i < fBytesList.Count; i++)
        //                    {
        //                        if (i == 0)
        //                        {
        //                            fReturnRead.TcpOperationCommand = Encoding.UTF8.GetString(fBytesList[i]);
        //                            fReadDataLenght -= fBytesList[i].Length;
        //                        }
        //                        else if (i == 1)
        //                        {
        //                            fReturnRead.Lenght = Convert.ToInt32(Encoding.UTF8.GetString(fBytesList[i]));
        //                            fReadDataLenght -= fBytesList[i].Length;
        //                        }
        //                        else
        //                        {
        //                            packetBuffer.AddRange(fBytesList[i]);
        //                        }
        //                    }
        //                }
        //            }
        //            else
        //            {
        //                packetBuffer.AddRange(TcpDataBytes);
        //            }
        //            fReadDataLenght += fbytesCount;
        //        }
        //        while (fReadDataLenght < fReturnRead.Lenght);
        //        fReturnRead.Message = OtherFun.BytesTrimEnd(packetBuffer.ToArray());
        //        fReturnRead.ReadStatus = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        fReturnRead.ReadStatus = false;
        //        fReturnRead.ErrorMsg = ex.ToString();
        //    }
        //    return fReturnRead;
        //}
        private TcpReadStruct ServerRead(int pClientIndex)
        {
            //回馈数据集
            TcpReadStruct fReturnRead = new TcpReadStruct()
            {
                ReadStatus = false,
                Lenght = 0,
            };
            //转换客户端
            TcpClient fTcpClient = (TcpClient)_TcpClientList[pClientIndex];
            if (fTcpClient.Client.Connected)
            {
                //定义数据流
                NetworkStream fStream = fTcpClient.GetStream();
                //设置超时时间
                fStream.ReadTimeout = TcpOperationCommand.TimeOut;
                fStream.WriteTimeout = TcpOperationCommand.TimeOut;

                //读取数据包的长度
                byte[] lengthBuffer = new byte[4];
                int bytesRead = 0;
                int length = 0;
                while (bytesRead < 4)
                {
                    int read = fStream.Read(lengthBuffer, bytesRead, 4 - bytesRead);
                    if (read == 0)
                    {
                        LogWrite(fTcpClient.Client.RemoteEndPoint.ToString(), "请先发送数据包长度");
                        return fReturnRead;
                    }
                    bytesRead += read;
                }
                length = BitConverter.ToInt32(lengthBuffer, 0);

                //读取主数据包
                byte[] dataBuffer = new byte[length];
                bytesRead = 0;
                while (bytesRead < length)
                {
                    int read = fStream.Read(dataBuffer, bytesRead, length - bytesRead);
                    if (read == 0)
                    {
                        LogWrite(fTcpClient.Client.RemoteEndPoint.ToString(), "没有数据包");
                        return fReturnRead;
                    }
                    bytesRead += read;
                }

                //处理数据
                List<byte[]> fBytesList = OtherFun.BytesSplitFor1(dataBuffer, TcpOperationCommand.ReadDataFenGe);
                if (fBytesList.Count == 2)
                {
                    fReturnRead.ReadStatus = true;
                    fReturnRead.TcpOperationCommand = Encoding.UTF8.GetString(fBytesList[0]);
                    fReturnRead.Message = fBytesList[1];
                }
            }
            else
            {
                LogWrite(fTcpClient.Client.RemoteEndPoint.ToString(), "读取失败，客户端连接异常");
            }
            return fReturnRead;
        }
        #endregion
        #region 业务处理功能
        /// <summary>
        /// 获取当前目录下的文件及目录
        /// </summary>
        /// <param name="pClientDirectoryPath">客户端当前目录</param>
        /// <returns></returns>
        private TcpWriteStruct GetList(string pClientDirectoryPath)
        {
            TcpWriteStruct fTcpStruct = new TcpWriteStruct()
            {
                IsByte = false,
                TcpOperationCommand = TcpOperationCommand.GetList,
            };

            List<FileListStruct> fList = new List<FileListStruct>();
            FileListStruct fileInfo;
            if (pClientDirectoryPath.Length == 0)
            {
                DriveInfo[] logicalDrives = DriveInfo.GetDrives();
                foreach (DriveInfo drive in logicalDrives)
                {
                    if ("C:\\" != drive.Name)
                    {
                        fileInfo = new FileListStruct();
                        fileInfo.name = drive.Name.TrimEnd('\\');
                        fileInfo.IsDirectory = true;
                        fileInfo.Lenght = 0;
                        fList.Add(fileInfo);
                    }
                }
                fTcpStruct.Message = JSONHelper.SerializeObject(fList);
            }
            else
            {
                if (Directory.Exists(pClientDirectoryPath))
                {
                    //列出所有文件夹
                    DirectoryInfo directoryInfo = new DirectoryInfo(pClientDirectoryPath);
                    DirectoryInfo[] directories = directoryInfo.GetDirectories();
                    foreach (DirectoryInfo directory in directories)
                    {
                        if (directory.Name.StartsWith("$") || directory.Attributes.ToString().IndexOf("Hidden") >= 0)
                        {
                            //过滤隐藏文件夹
                            continue;
                        }

                        fileInfo = new FileListStruct();
                        fileInfo.name = directory.Name;
                        fileInfo.IsDirectory = true;
                        //fileInfo.Lenght = 0;
                        fList.Add(fileInfo);
                    }

                    // 列出所有文件
                    FileInfo[] files = new DirectoryInfo(pClientDirectoryPath).GetFiles();
                    foreach (FileInfo file in files)
                    {
                        if (file.Name.StartsWith("$") || file.Attributes.ToString().IndexOf("Hidden") >= 0)
                        {
                            //过滤隐藏文件
                            continue;
                        }
                        fileInfo = new FileListStruct();
                        fileInfo.name = file.Name;
                        fileInfo.IsDirectory = false;
                        //fileInfo.Lenght = (int)file.Length / 1024 / 1024;
                        //fileInfo.CreateTime = file.CreationTime;
                        fList.Add(fileInfo);
                    }
                    fTcpStruct.Message = JSONHelper.SerializeObject(fList);
                }
                else
                {
                    fTcpStruct.TcpOperationCommand = TcpOperationCommand.Message;
                    fTcpStruct.Message = "没有找到对应的目录";
                }
            }
            return fTcpStruct;
        }
        /// <summary>
        /// 从当前目录进入下一级目录
        /// </summary>
        /// <param name="pDirectoryName">指定目录</param>
        /// <param name="pClientDirectoryPath">客户端当前目录</param>
        /// <returns></returns>
        private TcpWriteStruct EnterDirectory(string pDirectoryName, ref string pClientDirectoryPath)
        {
            TcpWriteStruct fTcpStruct = new TcpWriteStruct()
            {
                IsByte = false,
                TcpOperationCommand = TcpOperationCommand.EnterDir,
            };
            bool flag = false;
            if (pDirectoryName.Length > 0)
            {
                //服务器路径
                string fPatch = "";
                if (pClientDirectoryPath.Length == 0)
                {
                    fPatch = pDirectoryName;
                }
                else
                {
                    if (pClientDirectoryPath.EndsWith("\\"))
                    {
                        pClientDirectoryPath = pClientDirectoryPath.Substring(0, pClientDirectoryPath.Length - 2);
                    }
                    fPatch = pClientDirectoryPath + "\\" + pDirectoryName;
                }
                if (Directory.Exists(fPatch))
                {
                    flag = true;
                    pClientDirectoryPath = fPatch;
                    fTcpStruct.Message = flag.ToString();
                }
            }
            if (!flag)
            {
                fTcpStruct.TcpOperationCommand = TcpOperationCommand.Message;
                fTcpStruct.Message = "指定目录不存在";
            }
            return fTcpStruct;
        }
        /// <summary>
        /// 从当前目录返回到上级目录
        /// </summary>
        /// <param name="pClientDirectoryPath">客户端当前目录</param>
        /// <returns></returns>
        private TcpWriteStruct ComtOutDirectory(ref string pClientDirectoryPath)
        {
            TcpWriteStruct fTcpStruct = new TcpWriteStruct()
            {
                IsByte = false,
                TcpOperationCommand = TcpOperationCommand.ComeOutDir,
            };
            bool flag = false;
            if (pClientDirectoryPath.Length > 0)
            {
                string[] strDir = pClientDirectoryPath.Split('\\');
                if (strDir.Length == 1)
                {
                    pClientDirectoryPath = "";
                }
                else
                {
                    pClientDirectoryPath = String.Join("\\", strDir, 0, strDir.Length - 1);
                }
                flag = true;
                fTcpStruct.Message = flag.ToString();
            }
            else
            {
                if (!flag)
                {
                    fTcpStruct.TcpOperationCommand = TcpOperationCommand.Message;
                    fTcpStruct.Message = "已经是根目录";
                }
            }
            return fTcpStruct;
        }
        /// <summary>
        /// 获取当前目录下的指定图片
        /// </summary>
        /// <param name="pFileName">文件名称</param>
        /// <param name="pClientDirectoryPath">客户端当前目录</param>
        /// <returns></returns>
        private TcpWriteStruct GetPicZip(string pFileName, string pClientDirectoryPath)
        {
            TcpWriteStruct fTcpStruct = new TcpWriteStruct()
            {
                IsByte = true,
                TcpOperationCommand = TcpOperationCommand.GetPicZip,
            };
            bool flag = false;
            if (pFileName.Length > 0)
            {
                if (pFileName.ToUpper().Contains(".JPG") || pFileName.ToUpper().Contains(".BMP"))
                {
                    if (pClientDirectoryPath.EndsWith("\\"))
                    {
                        pClientDirectoryPath = pClientDirectoryPath.Substring(0, pClientDirectoryPath.Length - 2);
                    }
                    //服务器路径
                    string fPatch = pClientDirectoryPath + "\\" + pFileName;
                    if (File.Exists(fPatch))
                    {
                        flag = true;
                        //根据要求变更图版大小
                        byte[] fImgByte = PicHelper.ResizeAndConvertToBytes(fPatch);
                        fTcpStruct.Message = fImgByte;
                    }
                }
            }
            if (!flag)
            {
                fTcpStruct.TcpOperationCommand = TcpOperationCommand.Message;
                fTcpStruct.Message = "文件不存在";
            }
            return fTcpStruct;
        }
        #endregion
        #region 打印操作日志
        private void LogWrite(string pClientPointName, string pLogStr)
        {
            if (_RichBox.Text.Length > 50000)
            {
                WriteLogForFile.WirteTxt(_RichBox.Text);
                _RichBox.Text = "";
                _RichBox.Text += string.Format("{0} 清空日志", DateTime.Now);
            }
            if (pClientPointName.Length > 0)
            {
                _RichBox.Text += string.Format("\r\n{0} [{1}] {2}", DateTime.Now, pClientPointName, pLogStr);
            }
            else
            {
                _RichBox.Text += string.Format("\r\n{0} {1}", DateTime.Now, pLogStr);
            }
            //光标定位在最后一行
            _RichBox.SelectionStart = _RichBox.Text.Length;
            _RichBox.SelectionLength = 0;
            _RichBox.ScrollToCaret();
        }
        #endregion
    }
}
